package cn.flying.cloud.task.service.impl;

import java.io.Serializable;
import java.util.List;
import java.util.stream.Collectors;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import cn.flying.cloud.base.common.utils.Ft;
import cn.flying.cloud.base.common.vo.PageRequest;
import cn.flying.cloud.base.common.vo.PageResponse;
import cn.flying.cloud.task.bo.TaskJobLogBo;
import cn.flying.cloud.task.entity.TaskJobLog;
import cn.flying.cloud.task.mapper.TaskJobLogMapper;
import cn.flying.cloud.task.service.TaskJobLogService;

/**
 * 定时任务执行日志 服务实现类
 *
 * @author admin
 * @since 2025-09-24 10:53:53
 */
@Service
public class TaskJobLogServiceImpl extends ServiceImpl<TaskJobLogMapper, TaskJobLog> implements TaskJobLogService {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    /**
     * 查询总数
     *
     * @param param
     * @return
     */
    @Override
    public long countObj(TaskJobLogBo param) {
        return this.count(getLambdaQueryWrapper(param));
    }

    /**
     * 分页查询定时任务执行日志数据
     *
     * @param request
     * @return
     */
    @Override
    public PageResponse<TaskJobLogBo> getPageList(PageRequest<TaskJobLogBo> request) {
        TaskJobLogBo param = request.getParam();
        LambdaQueryWrapper<TaskJobLog> wrapper = getLambdaQueryWrapper(param);
        wrapper.orderByDesc(TaskJobLog::getCreatedTime);
        Page<TaskJobLog> page = this.page(new Page<>(request.getPageNum(), request.getPageSize()), wrapper);
        return new PageResponse<>(page.getTotal(), (int) page.getCurrent(), page.getRecords().stream().map(TaskJobLog::toBO).collect(Collectors.toList()));
    }

    /**
     * 构建查询对象
     *
     * @param param
     * @return
     */
    private LambdaQueryWrapper<TaskJobLog> getLambdaQueryWrapper(TaskJobLogBo param) {
        LambdaQueryWrapper<TaskJobLog> wrapper = new LambdaQueryWrapper<>();
        if (param != null) {
            if (Ft.String.isNotBlank(param.getSerialNo())) {
                wrapper.eq(TaskJobLog::getSerialNo, param.getSerialNo());
            }
            if (Ft.String.isNotBlank(param.getJobName())) {
                wrapper.likeLeft(TaskJobLog::getJobName, param.getJobName());
            }
            if (Ft.String.isNotBlank(param.getJobGroup())) {
                wrapper.likeLeft(TaskJobLog::getJobGroup, param.getJobGroup());
            }
            if (Ft.String.isNotBlank(param.getJobDesc())) {
                wrapper.likeLeft(TaskJobLog::getJobDesc, param.getJobDesc());
            }
            if (Ft.String.isNotBlank(param.getJobStatus())) {
                wrapper.eq(TaskJobLog::getJobStatus, param.getJobStatus());
            }
        }
        return wrapper;
    }

    /**
     * 列表查询定时任务执行日志数据
     *
     * @param param
     * @return
     */
    @Override
    public List<TaskJobLogBo> getList(TaskJobLogBo param) {
        LambdaQueryWrapper<TaskJobLog> wrapper = getLambdaQueryWrapper(param);
        // wrapper TODO
        List<TaskJobLog> list = this.list(wrapper);
        return list.stream().map(TaskJobLog::toBO).collect(Collectors.toList());
    }

    /**
     * 查询定时任务执行日志详情数据
     *
     * @param param
     * @return
     */
    @Override
    public TaskJobLogBo getOneObj(TaskJobLogBo param) {
        LambdaQueryWrapper<TaskJobLog> wrapper = getLambdaQueryWrapper(param);
        if (wrapper.isEmptyOfWhere()) {
            return new TaskJobLogBo();
        }
        return this.getOne(wrapper).toBO();
    }

    /**
     * 根据Id查询定时任务执行日志详情数据
     *
     * @param id
     * @return
     */
    @Override
    public TaskJobLogBo getObjById(Serializable id) {
        return this.getById(id).toBO();
    }

    /**
     * 新增定时任务执行日志数据
     *
     * @param param
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean createObj(TaskJobLogBo param) {
        param.setTenantId(Ft.Local.TenantId());
        TaskJobLog obj = new TaskJobLog();
        obj.fromBO(param);
        boolean saved = this.save(obj);
        param.setId(obj.getId());
        return saved;
    }

    /**
     * 更新定时任务执行日志数据
     *
     * @param param
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean updateObj(TaskJobLogBo param) {
        TaskJobLog obj = new TaskJobLog();
        obj.fromBO(param);
        return this.saveOrUpdate(obj);
    }

    /**
     * 删除定时任务执行日志数据
     *
     * @param param
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean deleteObj(TaskJobLogBo param) {
        LambdaQueryWrapper<TaskJobLog> wrapper = getLambdaQueryWrapper(param);
        // wrapper TODO
        return this.remove(wrapper);
    }

    /**
     * 根据Id删除定时任务执行日志数据
     *
     * @param id
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean deleteById(Serializable id) {
        return this.removeById(id);
    }
}
